#include "StdAfx.h"
#include "MsgSpliter.h"

MSG_BASE* CMsgSpliter::ExtractMsg()
{
	if (m_nLen < sizeof(int))
		return NULL;

	int nMsgLen = *((int*)m_szBuf);
	if (m_nLen < nMsgLen)
		return NULL;

	char* pMsg = new char[nMsgLen];
	memcpy(pMsg, m_szBuf, nMsgLen);

	char* pIter = m_szBuf + nMsgLen;
	m_nLen -= nMsgLen;
	memcpy(m_szBuf, pIter, m_nLen);

	return (MSG_BASE*)pMsg; //need to delete
}

int CMsgSpliter::RecvData(SOCKET sock)
{
	int nLeft = MAX_BUFFER - m_nLen;
	char* p = m_szBuf + m_nLen;
	int nLen = recv(sock, p, nLeft, 0);
	m_nLen += nLen;

	return nLen;
}